package com.yaozheng.vocationaltraining.adapter;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.bumptech.glide.Glide;
import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.activity.BaseActivity;
import com.yaozheng.vocationaltraining.iview.ITrainingBulletinView;
import com.yaozheng.vocationaltraining.service.NewsListService;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.ImageUtils;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.view.TrainingBulletinItemView;
import com.yaozheng.vocationaltraining.view.TrainingBulletinItemView_;

import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

/**
 * 培训公告设备器
 * 
 * @author Administrator
 * 
 */
public class TrainingBulletinAdapter extends BaseAdapter {
	private BaseActivity baseActivity;
	/**
	 * 数据
	 */
	JSONArray dataList;
	/**
	 * 图片选项
	 */
	// DisplayImageOptions options;
	BitmapLoader bitmapLoader;

	/**
	 * 新闻服务
	 */
	ITrainingBulletinView iTrainingBulletinView;

	public TrainingBulletinAdapter(BaseActivity baseActivity,
			JSONArray dataList, ITrainingBulletinView iTrainingBulletinView) {
		this.baseActivity = baseActivity;
		bitmapLoader = new BitmapLoader(Glide.with(baseActivity),
				R.drawable.default_news_image);
		this.dataList = dataList;
		// options = ImageUtils.getCourserDisplayImageOptions(baseActivity);
		this.iTrainingBulletinView = iTrainingBulletinView;
	}

	public JSONArray getDataList() {
		return dataList;
	}

	@Override
	public int getCount() {
		return TypeUtils.getJsonArraySize(dataList);
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	/**
	 * 得到最后id
	 * 
	 * @return
	 */
	public int getLastId() {
		if (dataList != null && TypeUtils.getJsonArraySize(dataList) > 0) {

			JSONObject itemData = TypeUtils.getJsonObject(dataList,
					TypeUtils.getJsonArraySize(dataList) - 1);
			if (itemData != null) {
				return TypeUtils.getJsonInteger(itemData, "id", -1);
			}
		}
		return -1;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TrainingBulletinItemView trainingBulletinItemView;
		if (convertView == null) {
			trainingBulletinItemView = TrainingBulletinItemView_
					.build(baseActivity);
			trainingBulletinItemView
					.setiTrainingBulletinView(iTrainingBulletinView);
		} else {
			trainingBulletinItemView = (TrainingBulletinItemView) convertView;
		}
		trainingBulletinItemView.loadData(position,
				TypeUtils.getJsonObject(dataList, position), bitmapLoader);
		return trainingBulletinItemView;
	}
}
